package com.study.service.impl;

import com.study.dao.AccountDao;
import com.study.pojo.Account;
import com.study.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("transferService")
public class TransferServiceImpl implements TransferService {

    //@Autowired 按照类型注入，如果类型无法确定唯一对象，可以结合@Qualifier 指定具体的id
    @Autowired
    @Qualifier("accountDao")
    private AccountDao accountDao;

    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {

        // 查询两个用户的账户信息
        Account from = accountDao.queryAccountByCardNo(fromCardNo);
        Account to = accountDao.queryAccountByCardNo(toCardNo);

        //对账户信息转账的金额进行处理
        from.setMoney(from.getMoney()-money);
        to.setMoney(to.getMoney()+money);

        //更新两个账户的金额
        accountDao.updateAccountByCardNo(to);
        accountDao.updateAccountByCardNo(from);
    }
}
